use serde::{Deserialize, Serialize};

use crate::{data::NodeData, graph::value::Value};

use super::{Input, NodeTrait, Output};

#[derive(Clone, Serialize, Deserialize)]
pub struct NormalNode {
    pub name: String,
    pub ty: String,
    pub description: Option<String>,
    pub inputs: Vec<Input>,
    pub outputs: Vec<Output>,
    pub has_children: bool,
    pub menu: Option<String>,
}

impl From<NodeData> for NormalNode {
    fn from(value: NodeData) -> Self {
        let mut outputs = Vec::new();
        if value.has_children {
            outputs.push(Output {
                name: "Child".to_string(),
                description: Some("子节点".to_string()),
                value: Value::Node
            });
        }
        outputs.extend(value.outputs.into_iter().map(|output| output.into()));
        let mut inputs = vec![Input {
            name: "Parent".to_string(),
            description: Some("父节点".to_string()),
            value: Value::Node
        }];
        inputs.extend(value.inputs.into_iter().map(|input| input.into()));
        Self {
            name: value.name,
            description: value.description,
            ty: value.ty,
            has_children: value.has_children,
            inputs,
            outputs,
            menu: value.menu,
        }
    }
}

impl NodeTrait for NormalNode {
    fn name(&self) ->  &str {
        &self.name
    }

    fn ty(&self) ->  &str {
        &self.ty
    }

    fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    fn inputs(&self) ->  &[Input] {
        &self.inputs
    }

    fn outputs(&self) ->  &[Output] {
        &self.outputs
    }

    fn has_children(&self) -> bool {
        self.has_children
    }

    fn inputs_mut(&mut self) ->  &mut Vec<Input> {
        &mut self.inputs
    }

    fn outputs_mut(&mut self) ->  &mut Vec<Output> {
        &mut self.outputs
    }
}
